package com.ywgt;

import java.util.Stack;

public class SingeLinkedList {
    public static void main(String[] args) {
     SingelList singelList=new SingelList();
        Node headNode = singelList.getHeadNode();
        //打印初始链表
        singelList.list(headNode);

     singelList.add(new Node(4));
     singelList.add(new Node(2));
     singelList.add(new Node(1));
     singelList.add(new Node(3));
        //打印添加后的链表
     //singelList.list(headNode);
        singelList.reversePrint(headNode);
     System.out.println("------------------------------------->");
     //singelList.delete(headNode,1);
     //Node node = singelList.reverseNode(headNode);
     //singelList.list(node);
     int count = singelList.count(headNode);
    }
}

class SingelList{
    private Node headNode=new Node(0);

    public Node getHeadNode() {
        return headNode;
    }

    /**
     * 向链表中添加节点
     * @param node
     */
    public void add(Node node){
        Node temp=headNode;
        if(temp.next==null){
            temp.next=node;
            return;
        }
        while (true) {
            if(temp.next==null){
                temp.next=node;
                return;
            }
            temp=temp.next;
        }
    }

    /**
     * 打印链表
     * @param headNode
     */
    public void list(Node headNode){
        Node temp=headNode;
        if(temp.next==null){
            System.out.println("链表为空");
            return;
        }
        while (true){
            if(temp.next==null){
                return;
            }
            System.out.println(temp.next);
            temp=temp.next;
        }
    }

    /**
     * 删除节点
     * @param headNode
     * @param no
     */
    public void delete(Node headNode,int no){
        Node temp=headNode;
        if(headNode.next==null){
            System.out.println("链表为空");
        }
        while(true) {
            if (no == temp.next.no) {
                temp.next = temp.next.next;
                break;
            } else {
                temp = temp.next;
            }
        }
    }

    /**
     * 链表长度
     * @param headNode
     * @return
     */
    public int count(Node headNode){
        Node temp=headNode;

        if(temp.next==null){
            return 0;
        }
        int count =0;

        while (true){
            if(temp.next==null){
                break;
            }else {
                count++;
                temp=temp.next;
            }
        }
        return count;
    }

    /**
     * 反转链表
     * @param headNode
     * @return
     */
    public Node reverseNode(Node headNode){
        if(headNode.next==null||headNode.next.next==null){
            return headNode;
        }
        Node cur=headNode.next;
        Node next=null;
        Node reverseHead=new Node(0);
        while(cur!=null){
            next=cur.next;
            cur.next=reverseHead.next;
            reverseHead.next=cur;
            cur=next;
        }
        headNode.next=reverseHead.next;
        return  headNode;
    }

    public void reversePrint(Node node){
        Stack<Node> nodes=new Stack<>();
        Node temp=node.next;
        while(temp!=null){
            nodes.push(temp);
            temp=temp.next;
        }
        while (!nodes.isEmpty()){
            System.out.println(nodes.pop());
        }
    }

  /*  public void update(Node headNode,Node node){
        Node temp=headNode;
        if(temp.next==null){
            return;
        }
        while(true) {
            if(temp.next.no==node.no){
                temp.next.***=node.***;
                return;
            }
            else {
                temp=temp.next;
            }
       }
    }*/
}


class Node{
    public int no;
    public Node next;

    public Node(int no) {
        this.no = no;
    }

    @Override
    public String toString() {
        return "Node{" +
                "no=" + no +
                '}';
    }
}
